import * as http from 'http'
import * as url from 'url'
import config from './config'

export function start(route, handle) {
	function onRequest(request, response) {
		//url.parse已被弃用
		// const pathName = url.parse(request.url).pathname
		const urlInstance = new url.URL(
			request.url,
			config.baseURL + ':' + config.port
		)
		const pathName = urlInstance.pathname
		console.log('Request for ' + pathName + ' received')

		route(handle, pathName, request, response)
	}
	http.createServer(onRequest).listen(config.port)
	console.log('Server has started')
}
